/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 * 暴力求解：三层循环嵌套
 */
var threeSumClosest = function (nums, target) {
  let sum = 0;
  let spanceSum = 0;
  let spance = null;
  for (let i = 0; i < nums.length - 2; i++) {
    for (let k = i + 1; k < nums.length; k++) {
      for (let y = k + 1; y < nums.length; y++) {
        sum = nums[i] + nums[k] + nums[y];

        if (spance == null) {
          spance = Math.abs(sum - target);
          spanceSum = sum;
        }
        if (Math.abs(sum - target) == 0) {
          return sum;
        } else if (Math.abs(sum - target) < spance) {
          spanceSum = sum;
          spance = Math.abs(sum - target);
        }
      }
    }
  }
  return spanceSum;
};

var nums = [0, 1, 2, -3, -12, 5, 6, 9, -11];
var target = 11;
let result = threeSumClosest(nums, target);
console.log(result);
